let routes = []
let keepAlives = []
let childrenMenu = {}
let allMenu = []

const scanPages = () => {
  // @link https://webpack.docschina.org/api/module-methods/#require-context
  const modulesFiles = require.context('@/pages/', true, /\.vue$/)
  modulesFiles.keys().forEach(modulePath => {
    const pageName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1') // order/create
    const component = modulesFiles(modulePath).default
    if (!component.name) {
      component.name = pageName.replace(/\//, '-')
    }
    const name = component.name ? component.name : pageName
    const meta = component.meta || { auth: true, title: component.title ? component.title : name, keepAlive: true, isMenu: true }
    const path = component.path ? component.path : `/${pageName}`

    let { keepAlive = true, group = null, isMenu = true, icon = '', title = name, menuIndex = null } = meta

    // 判断是否 缓存组件
    if (keepAlive) {
      keepAlives.push(component.name)
    } else {
      // if (typeof component.beforeRouteUpdate === 'undefined') {
      //   component.beforeRouteUpdate = function (to, from, next) {
      //     console.log('beforeRouteUpdate')
      //     if (typeof this._initFun === 'function') {
      //       this._initFun(to, from)
      //     }
      //     next()
      //   }
      // }
    }

    // 自动生成菜单
    if (pageName.includes('/') && isMenu) {
      if (!group) {
        group = pageName.split('/', 1)[0]
      }
      if (typeof childrenMenu[group] === 'undefined') {
        childrenMenu[group] = []
      }
      childrenMenu[group].push({
        title, icon, path, menuIndex
      })
    }

    routes.push({
      path,
      name,
      component,
      meta
    })
  })
}

const sortChild = (arr) => {
  let sortArr = []
  for (let item of arr) {
    if (typeof item['menuIndex'] === 'undefined' || item['menuIndex'] === null) {
      // 没有指定顺序直接往后排
      sortArr.push(item)
      continue
    }
    let insert = false
    for (let i in sortArr) {
      // 遇到没有指定 menuIndex 的  或者 menuIndex 大于当前的
      if (typeof sortArr[i]['menuIndex'] === 'undefined' || sortArr[i]['menuIndex'] === null || sortArr[i]['menuIndex'] > item['menuIndex']) {
        sortArr.splice(i, 0, item)
        insert = true
        break
      }
    }
    if (!insert) {
      sortArr.push(item)
    }
  }
  return sortArr
}

const rootMenu = [
  { title: '仪表盘', icon: 'el-icon-menu', path: '/dashboard' },
  {
    title: '订单',
    icon: 'el-icon-orange',
    group: 'order'
  },
  {
    title: '品牌',
    icon: 'el-icon-orange',
    group: 'brand'
  }
]

const computeMenu = () => {
  for (let item of rootMenu) {
    if (item.path) {
      allMenu.push(item)
      continue
    }
    let { group, children = [] } = item
    if (typeof childrenMenu[group] === 'undefined') {
      allMenu.push(item)
      continue
    }
    children = sortChild(children.concat(childrenMenu[group]))
    item['children'] = children
    allMenu.push(item)
  }
}
scanPages()
computeMenu()

export default {
  routes, keepAlives, allMenu
}
